<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<body>
<form id="treeButtonForm"  role="form" method="post" action="tree/update">
    <input type="hidden" name="pId" th:value="${entity.tree.id}" />
    <input type="hidden" name="id" id="treeId" th:value="${entity.id}" />
    <input type="hidden" name="type" value="2" />
    <div class="row">
        <div class="col-xs-6" >
            <label >父菜单名称:</label>
            <input type="text" class="form-control" th:value="${entity.tree.name}" readonly="readonly"  />
        </div>
        <div class="col-xs-6">
            <label for="name">按钮名称:</label>
            <input type="text" class="form-control" name="name" id="name"  th:value="${entity.name}" placeholder="请输入菜单名称" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <label for="code">按钮编码:</label>
            <input type="text" class="form-control" name="code" id="code" th:value="${entity.code}"  placeholder="请输入菜单编码" />
        </div>
        <div class="col-xs-6">
            <label >按钮状态:</label>
            <div class="col-xs-12">
                <label  class='radio-inline'><input type='radio' name='state' value='1' th:checked="${entity.state} == 1?true:false"   />可用</label>
                <label  class='radio-inline'><input type='radio' name='state' value='2' th:checked="${entity.state} == 2?true:false"   />禁用</label>
            </div>
        </div>
    </div>
</form>
<script th:inline="javascript">
    <![CDATA[
    $(function () {
        $('#treeButtonForm',window.top.document).bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    message: '按钮名称验证失败',
                    validators: {
                        notEmpty: {
                            message: '按钮名称不能为空'
                        }
                    }
                },
                code: {
                    message: '按钮编码验证失败',
                    validators: {
                        notEmpty: {
                            message: '按钮编码不能为空'
                        },
                        threshold :  2 ,
                        remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值，获得一个json数据。例表示正确：{"valid",true}
                            url: "tree/isExist",//验证地址
                            data:function(validator) {// 获取需要传送到后台的验证的数据
                                return {
                                    type : 2,
                                    code:$("#code").val(),
                                    id:$("#treeId").val()
                                }
                            },
                            message: '按钮编码已存在',//提示消息
                            delay :  500,//每输入一个字符，就发ajax请求，服务器压力还是太大，设置2秒发送一次ajax（默认输入一个字符，提交一次，服务器压力太大）
                            type: 'POST'//请求方式
                        }
                    }
                }
            }
        })

        // 绑定dialog的确定按钮的监听事件
        $("#btnOk",window.top.document).click(function() {
            $("#btnOk",window.top.document).attr("disabled",true);
            // 此段是为防止需要点击两次按钮来实现验证的方法，若不添加此处的放行，那么我们将要点击两次确定按钮才可以提交验证
            var code = $("#code",window.top.document).val();
            // 判断当前的name有值，且当前不存在错误验证方可放开该字段的验证
            if(code != null && code != ""&&$("#code",window.top.document).parent("div").find('.glyphicon-remove').length==0){
                $('#treeButtonForm',window.top.document).bootstrapValidator('enableFieldValidators', 'code', false);
            } else {
                $('#treeButtonForm',window.top.document).bootstrapValidator('enableFieldValidators', 'code', true);
            }
            var bootstrapValidator = $("#treeButtonForm", window.top.document).data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid()){
                $.post($("#treeButtonForm",window.top.document).attr('action'),$("#treeButtonForm",window.top.document).serialize(),function(e){
                    if(e.result){
                        refreshDoQuery();
                        window.Ewin.alert({message:'修改数据成功!'});
                    }else{
                        window.Ewin.alert({message:'修改数据失败!'});
                    }
                    $("#btnOk",window.top.document).attr("disabled",false);
                })
            }else{
                $("#btnOk",window.top.document).attr("disabled",false);
            }
        });

        // 重新刷新当前查询页的数据
        function refreshDoQuery(){
            var iframes = $(window.parent.document).contents().find(".J_iframe");
            for(var i=0;i<iframes.length;i++){
                if($(iframes[i]).css("display")=='inline'){
                    $('.modal-dialog', window.top.document).parent('div').remove()
                    $('body', window.top.document).find('.modal-backdrop').remove();
                    iframes[i].contentWindow.doQuery();
                    break;
                }
            }
        }

    })
    ]]>
</script>
</body>
</html>